package com.zhilingshenghuo.app.zhly;

import android.annotation.SuppressLint;
import android.content.Context;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.multidex.BuildConfig;
import androidx.multidex.MultiDex;

import com.zhilingshenghuo.app.zhly.di.component.ApplicationComponent;
import com.zhilingshenghuo.app.zhly.di.component.DaggerApplicationComponent;
import com.zhilingshenghuo.app.zhly.di.modules.ApplicationModule;
import com.zhilingshenghuo.app.zhly.utils.Constants;
import com.zhilingshenghuo.app.zhly.utils.MyLifecycleHandler;
import com.kc.openset.OSETSDK;
import com.kc.openset.listener.OSETInitListener;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.uuzuche.lib_zxing.ZApplication;

import cat.ereza.customactivityoncrash.config.CaocConfig;


public class AndroidApplication extends ZApplication {
    private ApplicationComponent applicationComponent;

    @SuppressLint("StaticFieldLeak")

    private static Context mContext;

    @SuppressLint("StaticFieldLeak")
    private static AndroidApplication instance;

    public static Context getContext() {
        return mContext;
    }

    public static AndroidApplication getInstance() {
        return instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        instance = this;
        initializeInjector();
//        initAlbum();
        initCrash();
        initOpenSet();
        registerActivityLifecycleCallbacks(new MyLifecycleHandler());

    }

//    private void initAlbum() {
//        Album.initialize(AlbumConfig.newBuilder(this)
//                .setAlbumLoader(new MediaLoader())
//                .build());
//    }

    private void initializeInjector() {
        this.applicationComponent = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .build();
    }

    private void initOpenSet() {
        //初始化最新openSet广告位SDK
        OSETSDK.getInstance().init(this, Constants.OPEN_SET_APP_KEY, new OSETInitListener() {
            @Override
            public void onError(String s) {

            }

            @Override
            public void onSuccess() {

            }
        });
    }

    private void initCrash() {
        CaocConfig.Builder.create()
                .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT)
                .enabled(!BuildConfig.DEBUG)
                .errorActivity(MainActivity.class)
                .apply();
    }

    public ApplicationComponent getApplicationComponent() {
        return this.applicationComponent;
    }

    static {
        //启用矢量图兼容
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new MaterialHeader(context));
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context).setDrawableSize(20f).setTextSizeTitle(14F));
    }
}
